package com.bawei.home.ui

import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bawei.home.R
import com.bawei.home.databinding.FragmentHomeBinding
import com.bawei.home.model.SimpleVideoModel
import com.bw.lib_mvvm.entity.SimpleVideo
import com.bw.lib_mvvm.BaseFragment
import com.bw.lib_mvvm.entity.TypeData


class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    lateinit var viewModel:SimpleVideoModel
    lateinit var simpleadapter: HomeAdapter
    lateinit var typeAdapter: HomeTypeAdapter
    private var simleList= mutableListOf<SimpleVideo>()
    private var typeList= mutableListOf<TypeData>()
    override fun bindLayout(): Int = R.layout.fragment_home

    override fun initView() {
        viewModel=ViewModelProvider(this)[SimpleVideoModel::class.java]
        viewModel.getVideoModel(1,10)
        viewModel.success.observe(this){
            simleList.clear()
            simleList.addAll(it.data)
            simpleadapter.notifyDataSetChanged()
        }
        viewModel.getTypeModel()
        viewModel.typesuccess.observe(this){
            typeList.clear()
            typeList.addAll(it.data)
            simpleadapter.notifyDataSetChanged()
        }

    }

    override fun initData() {
        simpleadapter= HomeAdapter(simleList)
//        viewDataBinding.videoRv.layoutManager= GridLayoutManager(requireContext(),2)
        viewDataBinding.videoRv.layoutManager= StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        viewDataBinding.videoRv.adapter=simpleadapter

        typeAdapter=HomeTypeAdapter(typeList)
//        viewDataBinding.typeRv.layoutManager=LinearLayoutManager(requireContext(),GridLayoutManager.HORIZONTAL,false)
        viewDataBinding.typeRv.layoutManager=GridLayoutManager(requireContext(),GridLayoutManager.VERTICAL)
        viewDataBinding.typeRv.adapter=typeAdapter

//        binding.tbHomeTitlebar.setOnClickListener(object : TitleBar.OnClickListener {
//            override fun onClick(view: View?) {
//                getBaseActivity().route(SearchActivity::class.java)
//            }
//        })
        simpleadapter.setOnItemClickListener { adapter, view, position ->
            var intent=Intent(requireContext(),DetailActivity::class.java)
            intent.putExtra("videopath",simleList[position].videopath)
            startActivity(intent)
        }



    }


}